﻿{extend name="../application/index/view/index/base.html" /}

{block name="main-content"}
<div class="breadcrumbs" id="breadcrumbs">
	<script type="text/javascript">
        try{ace.settings.check('breadcrumbs' , 'fixed')}catch(e){}
	</script>

	<ul class="breadcrumb">
		<li>
			<i class="icon-home home-icon"></i>
			<a href="#">用户管理</a>
		</li>
		<li class="active">用户信息</li>
	</ul><!-- .breadcrumb -->

	<div class="nav-search" id="nav-search">
		<form class="form-search">
									<span class="input-icon">
										<input type="text" placeholder="Search ..." class="nav-search-input" id="nav-search-input" autocomplete="off" />
										<i class="icon-search nav-search-icon"></i>
									</span>
		</form>
	</div><!-- #nav-search -->
</div>
<div class="page-content">
	<style>
		.tr th {
			text-align: center;
		}

		.notice {
			font-size: 16px;
		}
	</style>


	<div class="page-body" style="padding-top: 10px;">
		<div class="table-toolbar">
			<button id="police_station_add" class="btn btn-primary" data-toggle="modal" data-target="#add_admin" style="border-radius: 5px 5px">
				新增派出所信息
			</button>

			<button id="admin_query" class="btn btn-primary" style="float: right;margin-right: 24px;">
				查询信息</button>
			<input type="text" placeholder="--请输入省/市/区进行查询--" id="query" name="file_id"
				   style="padding-left:8px;width: 300px;height: 42px;float: right">
		</div>
		<table class="table table-striped table-hover table-bordered" id="admin" style="margin-top:12px;text-align: center;">
			<thead>
			<tr role="row" class="tr">
				<th>
					省份
				</th>
				<th>
					城市
				</th>
				<th>
					区/县
				</th>
				<th>
					派出所
				</th>

				<th>
					创建时间
				</th>
				<th>
					操作
				</th>
			</tr>
			</thead>
			<tbody id="tb_insert">

			</tbody>
		</table>
		<div id="pagination_div"></div>
	</div>

</div><!-- /.page-content -->
<div class="modal fade" id="add_admin" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	<div class="modal-dialog ui-widget-content">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-hidden="true">
					&times;
				</button>
				<h4 class="modal-title">
					新增信息
				</h4>
			</div>
			<div class="modal-body">
				<div style="margin-top: 10px">
					<span class="notice" style="margin-right: 23px">省份名称 :</span>
					<input type="text" placeholder="--省份名称必填，只能为汉字--" id="province" name="file_id"
						   style="padding-left:16px;width: 400px;height: 40px;">
					<span style="color: red">*</span>
				</div>
				<div style="margin-top: 10px">
					<span class="notice" style="margin-right: 48px">市名：</span>
					<input type="text" placeholder="--市名必填，只能为汉字--" id="city" name="file_name"
						   style="padding-left:16px;width: 400px;height: 40px;">
					<span style="color: red">*</span>
				</div>
				<div style="margin-top: 10px">
					<span class="notice" style="margin-right: 9px">区/县名称：</span>
					<input type="text" placeholder="--区/县名称必填，只能为汉字--" id="region" name="class"
						   style="padding-left:16px;width: 400px;height: 40px;">
					<span style="color: red">*</span>
				</div>
				<div style="margin-top: 10px">
					<span class="notice">派出所名称：</span>
					<input type="text" placeholder="--派出所名称必填，只能为汉字--" id="police_station" name="major"
						   style="padding-left:16px;width: 400px;height: 40px;">
					<span style="color: red">*</span>
				</div>
			</div>
			<div class="modal-footer">
				<button type="button" class="btn btn-default" data-dismiss="modal" id="false">取消
				</button>
				<button type="button" class="btn btn-primary" id="addBtn">
					确定
				</button>
			</div>
		</div><!-- /.modal-content -->
	</div><!-- /.modal -->
</div>
<script id="tpl-user" type="text/html">
	<% if(user.length>0){ %>
		<% for(var i in user) {%>
		<tr class="tr">
			<td class="province"><%= user[i]["province"] %></td>
			<td class="city"><%= user[i].city %></td>
			<td class="region"><%=user[i].region%></td>
			<td class="police_station"><%=user[i].policeStation%></td>
			<td class="create_time"><%=user[i].create_time%></td>
			<td>
				<button class="btn btn-info btn-xs update" data-toggle="modal"
						data-target="#add_admin">
					<i class="fa fa-edit"></i> 修改
				</button>
				<button class="btn btn-danger btn-xs del" data-toggle="modal" data-target="#delete">
					<i class="fa fa-trash-o"></i> 删除
				</button>

			</td>
		</tr>;
		<% } %>
	<% }else{ %>
	<tr class="tr" >
		<td colspan="6">暂无数据！</td>
	</tr>
	<% } %>
</script>

<script>
    var limit = 5;
    var isNew = true;
    $(function () {
        function initPagination(total) {
            var options = {
                currentPage: 1,
                totalPages: total,
                onPageClicked: function (e, originalEvent, type, page) {
                    refresh(page);

                }
            };
            $('#pagination_div').bootstrapPaginator(options);
        }
        //设置总页数
        function getPageLimit() {
            $.get('/index.php/index/police_station/getPageLimit', function (data) {
                if (data.code == 200) {
                    var count = data.data;
                    var pageSum = Math.ceil(count / limit) == 0 ? 1 : Math.ceil(count / limit);
                    initPagination(pageSum);
                } else {
                    initPagination(1);
                }
            });
        }

        //条件查询设置总页数
        function getQueryPageLimit() {
            $.get('/index.php/index/police_station/getQueryPageLimit',
                {
                    query:$('#query').val()

                }, function (data) {
                    if (data.code == 200) {
                        var count = data.data;
                        var pageSum = Math.ceil(count / limit) == 0 ? 1 : Math.ceil(count / limit);
                        initPagination(pageSum);
                    } else {
                        initPagination(1);
                    }
                });
        }

        //刷新函数
        function refresh(page) {
            var param = {
                page: page,
                limit: limit,
                query:$("#query").val(),
            };
            $.get('/index.php/index/police_station/query',param,function (data) {
                $("#tb_insert").find('.tr').remove();

                var data = data.data;
                var html = template('tpl-user', {
                    user: data
                });
                $("#tb_insert").append(html);
            });
            initPagination();
        }
        

        //新增派出所信息
        $("#police_station_add").click(function () {
            $('#province').removeAttr("readonly");
            $('#city').removeAttr("readonly");
            $('#region').removeAttr("readonly");
            if ($("#province").val() != '' || $("#city").val() != '' || $("#region").val() != '' || $("#police_station").val() != '') {
                $('#province').val('');
                $('#city').val('');
                $('#region').val('');
                $('#police_station').val('');
            }
        });

        //确认信息添加与修改
        $("#addBtn").click(function () {
            if(isNew == true){
                $.post('/index.php/index/police_station/save',{
                    province:$("#province").val(),
                    city:$("#city").val(),
                    region:$("#region").val(),
                    policeStation:$("#police_station").val(),
                },function ($data) {
					if ($data['code'] == 200) {
                        refresh();
                        layer.msg("添加成功");
                        $("#add_admin").modal('hide')
                    } else if ($data['code'] == 400) {
                        layer.msg($data['msg']);

					} else {
                        layer.msg($data['msg']);
                    }

                })
            }else{
                $.post('/index.php/index/police_station/update',{
                    province:$("#province").val(),
                    city:$("#city").val(),
                    region:$("#region").val(),
                    policeStation:$("#police_station").val(),
                },function ($data) {
                    if ($data['code'] == 200) {
                        refresh();
                        layer.msg("修改成功");
                        $("#add_admin").modal('hide')
                    } else {
                        layer.msg("修改失败");
                    }
                })
            }
        });

        //修改信息
        $(document).on("click", ".update", function () {
            isNew = false;
            $("#province").attr("readonly","readonly");
            $("#city").attr("readonly","readonly");
            $("#region").attr("readonly","readonly");
            $('.modal-title').html('修改信息');
            var varible = $(this).parent().parent();
            $("#province").val(varible.find(".province").html());
            $("#city").val(varible.find(".city").html());
            $("#region").val(varible.find(".region").html());
            $("#police_station").val(varible.find(".police_station").html());
        });

        //查询信息
        $('#admin_query').click(function () {
            if($("#query").val() == ''){
                layer.msg("请输入搜索条件进行查询！")
            }else{
                refresh(1);
                getQueryPageLimit();
                $("#query").val('');
            }

        });



        //删除信息
        $(document).on("click", ".del", function () {
            delete_it = $(this).parent().parent().find('.police_station').html();
            layer.confirm('您确定要删除吗？', {
                btn: ['确定', '取消'] //按钮
            }, function () {
                $.post("/index.php/index/police_station/del", {
                    policeStation: delete_it

                }, function () {
                    layer.msg('删除成功');
                    refresh();
                });
            }, function () {
                return;
            });
        });

        //页面加载初始化函数
        refresh(1);
        getPageLimit();
    });

</script>


{/block}

